var app = getApp();
// pages/productDetail/productDetail.js
Page({
   
    /**
     * 页面的初始数据
     */
    data: {
        productObj:'',
        type:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.info(options)
        this.init(options)
        this.setData({
            type:options.type
        })
    },
    init(options) {
        console.info(123)
        let that=this
        // let APPCODE = 'de583a7d32dd475eb649a76652b38129'
        // let APPCODE = 'b570317543cc44fc8151c496cd36613e'
        let APPCODE = '97ebd3f116914fcc82803b5cddf435fc'
        let data = {
          'START_CITY': options.START_CITY,
          'END_CITY': options.END_CITY,
          'END_DATE': '',
          'START_DATE': options.START_DATE,
          'AppCode': APPCODE
        }
          console.log(options.type)
          console.log(data)
        if (options.type == 1) {
          console.log(this.type)
          data.END_DATE = options.END_DATE
        }
        console.log(data)
        wx.request({
            url: 'https://plane.market.alicloudapi.com/ai_market/ai_airplane/get_airplane_list',
            method: 'get',
            data: data,
            header: {
                'Authorization': `APPCODE ${APPCODE}`
            },
            success: function (res) {
                console.info('res', res)
                if(res.statusCode==200){
                  for(let index in res.data.FLIGHTS){
                    res.data.FLIGHTS[index].interval = that.timeinterval(res.data.FLIGHTS[index].START_DATE,
                      res.data.FLIGHTS[index].START_TIME,
                      res.data.FLIGHTS[index].END_DATE,
                      res.data.FLIGHTS[index].END_TIME)
                  }
                console.info('res', res)

                    that.setData({
                        productObj:res.data
                    })
                }

            },
            fail: function (error) {
                console.info('error', error)
            }
        })
    },

    sortByInterval: function(){
      
    },
    timeinterval: function(startDate,startTime,endDate,endTime){
      let startTime1 = new Date(startDate.slice(0,4),startDate.slice(4,6),startDate.slice(6,8),startTime.split(":")[0],startTime.split(":")[1]); // 开始时间
      let endTime1 = new Date(endDate.slice(0,4),endDate.slice(4,6),endDate.slice(6,8),endTime.split(":")[0],endTime.split(":")[1]); // 开始时间
       let usedTime = endTime1 - startTime1; // 相差的毫秒数
       let days = Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数
       let leavel = usedTime % (24 * 3600 * 1000); // 计算天数后剩余的时间
       let hours = Math.floor(leavel / (3600 * 1000)); // 计算剩余的小时数
       let leavel2 = leavel % (3600 * 1000); // 计算剩余小时后剩余的毫秒数
       let minutes = Math.floor(leavel2 / (60 * 1000)); // 计算剩余的分钟数
       if (days == 0) {
         return hours + '时' + minutes + '分'
       }
       if (days < 0) {
         return (hours+24)+'时'+(minutes+60)+'分'
       }
       return days + '天' + hours + '时' + minutes + '分';
      },
    
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },


})
